package com.awspaas.user.apps.cpq.event;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class templatePriceFormAfterSave extends ExecuteListener{
	public templatePriceFormAfterSave() {
		setDescription("项目报价标准模板中，如果数据为创建新版本和创建新模板的时候将状态更新为启用");
	}
	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		try {
			String bindId=pec.getProcessInstance().getId();
			//BO表名
	        String boName = pec.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
	        if(boName.equals("BO_EU_CPQ_TEMPLATE_PRICE")) {
	        	BO data=SDK.getBOAPI().query("BO_EU_CPQ_TEMPLATE_PRICE").bindId(bindId).detail();
	        	String type=data.getString("CJLX");
	        	
	        	if(type.equals("创建新版本")) {
	        		String xmlx=data.getString("XMLX");
	        		String cpxh=data.getString("CPXH");
	        		String dzsm=data.getString("DZSM");
	        		SimpleDateFormat sdftime = new SimpleDateFormat("yyyy-MM-dd");
	        		long newtime = new Date().getTime();// 创建时间
					String timestamp=sdftime.format(new Date(newtime));
	        		String sql="Update BO_EU_CPQ_TEMPLATE_PRICE set STATUS='停用', TYSJ='"+timestamp+"' where XMLX='"+xmlx+"' and CPXH='"+cpxh+"'  and DZSM='"+dzsm+"' and STATUS='启用'";
	        		DBSql.update(sql);
	        		data.set("STATUS", "启用");
		        	data.set("CJLX", "");
		        	SDK.getBOAPI().update("BO_EU_CPQ_TEMPLATE_PRICE", data);
	        	}else if (type.equals("创建新模板")) {
	        		data.set("STATUS", "启用");
		        	data.set("CJLX", "");
		        	SDK.getBOAPI().update("BO_EU_CPQ_TEMPLATE_PRICE", data);
				}
	        	
	        }
		} catch (Exception e) {
			throw e;
		}
		
	}

}

